home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / Hydra11s.lha / HBBS / Source / Test / RType / Main.C < prev    next >
C/C++ Source or Header  |  1996-06-25  |  3KB  |  181 lines

  1. /*
  2.  
  3.   RType (C) 1995 Dominic Clifton!
  4.  
  5.   yoikes, it's a rather handy for reading file backwards!!  just go
  6.   rtype <filename>..
  7.  
  8.   Great...
  9.  
  10.  
  11. */
  12.  
  13.  
  14. /* **** Includes *********************************************************** */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/memory.h>
  18. #include <dos/dos.h>
  19. #include <clib/exec_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include <clib/alib_protos.h>
  22.  
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <time.h>
  28.  
  29. /* **** Variables ********************************************************** */
  30.  
  31.  
  32. char   outstr[1024];
  33.  
  34. /* **** Functions ********************************************************** */
  35.  
  36.  
  37. #ifdef __SASC
  38. int CXBRK(void) { return(0); }
  39. int _CXBRK(void) { return(0); }
  40. void chkabort(void) {}
  41. #endif
  42.  
  43. BOOL FGetsR(BPTR FH,UBYTE *Buffer,LONG BufferLen)
  44. {
  45.  
  46.   LONG nr,startpos,where,back,foundpos=0;
  47.   BOOL retval=TRUE,sof=FALSE,error=FALSE;
  48.  
  49.   UBYTE *strptr=NULL;
  50.  
  51.  
  52.   Buffer[0]=0;
  53.   BufferLen--;
  54.  
  55.   where=Seek(FH,0,OFFSET_CURRENT);  // start of file already ?
  56.   if (where<=0) //start of file or error
  57.   {
  58.     retval=FALSE;
  59.   }
  60.   else
  61.   {
  62.     startpos=where; // make a note of where we started from.
  63.     do
  64.     {
  65.       back=255;
  66.       if (where<255) back=where;
  67.  
  68.       if (back>BufferLen) back=BufferLen;
  69.  
  70.       if ((where=Seek(FH,0-back,OFFSET_CURRENT))!=-1)
  71.       {
  72.         where-=back;
  73.  
  74.         if (nr=Read(FH,Buffer,back))
  75.         {
  76.           Buffer[nr]=0;
  77.           if (strptr=strrchr(Buffer,'\n'))
  78.           {
  79.             foundpos=where+(strptr-Buffer);
  80.  
  81.  
  82.             if (Seek(FH,foundpos+1,OFFSET_BEGINNING)==-1)
  83.             {
  84.               error=TRUE;
  85.             }
  86.           }
  87.           else
  88.           {
  89.             if (where==0)
  90.             {
  91.               if (Seek(FH,0,OFFSET_BEGINNING)==-1)
  92.               {
  93.                 error=TRUE;
  94.               }
  95.               else
  96.               {
  97.                 sof=TRUE;
  98.                 foundpos=1;
  99.               }
  100.             }
  101.             else
  102.             {
  103.  
  104.               if (Seek(FH,0-nr,OFFSET_CURRENT)!=-1)
  105.               {
  106.                 where-=nr;
  107.               }
  108.               else
  109.               {
  110.                 error=TRUE;
  111.               }
  112.             }
  113.  
  114.           }
  115.         }
  116.         else
  117.         {
  118.           error=TRUE;
  119.         }
  120.  
  121.       }
  122.     } while (strptr==NULL && !error && !sof);
  123.  
  124.     Buffer[0]=0;
  125.     if (strptr || sof)
  126.     {
  127.       FGets(FH,Buffer,BufferLen);
  128.       Seek(FH,(foundpos) ? foundpos-1 : 0 ,OFFSET_BEGINNING);
  129.     }
  130.   }
  131.  
  132.   return(retval);
  133.  
  134. }
  135.  
  136. void stripCR(UBYTE *String)
  137. {
  138.   ULONG Len;
  139.   if (String && (Len=strlen(String)))
  140.   {
  141.     Len--; // Len now = character index..
  142.  
  143.     while (Len && (String[Len]=='\r' || String[Len]=='\n'))
  144.     {
  145.       String[Len--]=0;
  146.     }
  147.   }
  148. }
  149.  
  150.  
  151.  
  152. int main(int argc,char **argv)
  153. {
  154.   BPTR FH;
  155.   UBYTE Buffer[255];
  156.  
  157.   if (argc==2)
  158.   {
  159.     if (argv[1][0]=='?')
  160.     {
  161.       puts("R-Type (C) 1996 Dominic Clifton - Hydra/LSD\n\n"
  162.            "this small util just reads file backwards line by line\n"
  163.            "and prints it on the screen\n\n"
  164.            "usage: RType <filename>");
  165.     }
  166.     else
  167.     {
  168.       if (FH=Open(argv[1],MODE_OLDFILE))
  169.       {
  170.         Seek(FH,0,OFFSET_END);
  171.         while (FGetsR(FH,Buffer,255))
  172.         {
  173.           stripCR(Buffer);
  174.           puts(Buffer);
  175.         }
  176.         Close(FH);
  177.       }
  178.     }
  179.   }
  180. }
  181.